//
//  FoodViewController.m
//  foodlist
//
//  Created by HappyMilk 02 on 2017/8/17.
//  Copyright © 2017年 tomi. All rights reserved.
//

#import "FoodViewController.h"
#import "HeadViewCell.h"
#import "MaterialViewCell.h"
#import "ProcessViewCell.h"
#import <UIImageView+WebCache.h>
#import "LoginHeader.h"
#import "MJLocaStorage.h"
#import <SVProgressHUD.h>

@interface FoodViewController ()<UITableViewDelegate ,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tabelview;
@property (weak, nonatomic) IBOutlet UILabel *lab_title;

@end

@implementation FoodViewController

- (IBAction)popvc:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.lab_title.text = self.foodmodel.name;
    [self.tabelview reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return self.foodmodel.material.count;
    }else{
        return self.foodmodel.process.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
        HeadViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HeadViewCell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"HeadViewCell" owner:nil options:nil] lastObject];
        }
        [cell.imgv_main sd_setImageWithURL:[NSURL URLWithString:self.foodmodel.pic]];
        cell.lab_title.text = self.foodmodel.name;
        [cell.btn_collect addTarget:self action:@selector(collectAction) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }else if (indexPath.section == 1){
        materialModel *model = self.foodmodel.material[indexPath.row];
        MaterialViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MaterialViewCell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MaterialViewCell" owner:nil options:nil] lastObject];
        }
        cell.lab_name.text = model.mname;
        cell.lab_num.text = model.amount;
        return cell;
    }else{
        processModel *model = self.foodmodel.process[indexPath.row];
        ProcessViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProcessViewCell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ProcessViewCell" owner:nil options:nil] lastObject];
        }
        [cell.imgv_pro sd_setImageWithURL:[NSURL URLWithString:model.pic]];
        cell.lab_title.text = [NSString stringWithFormat:@"%ld、%@",indexPath.row+1,model.pcontent];
        CGRect rect = [cell.lab_title.text boundingRectWithSize:(CGSize){__kwidth-40,MAXFLOAT} options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
        model.height = [NSString stringWithFormat:@"%.f",rect.size.height+153];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 175;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (IS_IPAD) {
            return 600;
        }
        return 280;
    }else if (indexPath.section == 1){
        return 45;
    }else{
        processModel *model = self.foodmodel.process[indexPath.row];
        return [model.height floatValue];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}


- (void)collectAction{
    
    if ([[MyAdTools shareInstance] showInterstitialWithVC:self]) {
        return;
    }
    
    NSMutableDictionary *dic = [self.foodmodel mj_keyValues];
    [dic removeObjectForKey:@"debugDescription"];
    [dic removeObjectForKey:@"description"];
    [dic removeObjectForKey:@"hash"];
    [dic removeObjectForKey:@"superclass"];
    for (int i=0; i<self.foodmodel.material.count; i++) {
        [dic[@"material"][i] removeObjectForKey:@"debugDescription"];
        [dic[@"material"][i] removeObjectForKey:@"description"];
        [dic[@"material"][i] removeObjectForKey:@"hash"];
        [dic[@"material"][i] removeObjectForKey:@"superclass"];
    }
    for (int i=0; i<self.foodmodel.process.count; i++) {
        [dic[@"process"][i] removeObjectForKey:@"debugDescription"];
        [dic[@"process"][i] removeObjectForKey:@"description"];
        [dic[@"process"][i] removeObjectForKey:@"hash"];
        [dic[@"process"][i] removeObjectForKey:@"superclass"];
    }
    
    
    NSMutableArray *muarr = [[MJLocaStorage getArrayFile:@"CollectList"] mutableCopy];
    [muarr addObject:dic];
    [MJLocaStorage saveArrayFile:[muarr copy] AndFileName:@"CollectList"];
    
    [SVProgressHUD showSuccessWithStatus:@"收藏成功！"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
